package test.sign;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

import cn.elricyu.tt.util.Base64Util;

public class SignProvider {
	private SignProvider() {
	}

	public static boolean verify(String pubKeyText, String plainText,
			String signText) {
		try {
			X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(
					Base64Util.decode(pubKeyText.getBytes()));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec);
			byte[] signed = Base64Util.decode(signText.getBytes());
			Signature signatureChecker = Signature.getInstance("MD5withRSA");
			signatureChecker.initVerify(pubKey);
			signatureChecker.update(plainText.getBytes());
			if (signatureChecker.verify(signed)) {
				return true;
			} else {
				return false;
			}
		} catch (Throwable e) {
			e.printStackTrace();
			return false;
		}
	}
}